﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Xml.Linq;
using Foothill.Utils;
using GGFW.BusinessLogic.SystemManage;


namespace GGFW.Web.Common
{
    public partial class GetUserMenuTree : System.Web.UI.Page
    {

        List<ExtTreeNode> list;
        List<GGFW.Entity.SystemManage.FoothillMenu> menuList;

        protected void Page_Load(object sender, EventArgs e)
        {
            string MenuID = Request.QueryString["MenuID"];

            // 获取菜单Tree的读取方法
            string json = "";
            Response.ContentType = "text/html";
            FoothillMenuControl foothillMenuControl = new FoothillMenuControl();
            Foothill.Privilege.Entity.FoothillUser usr = Foothill.Privilege.Utils.PrivilegeHttpUtil.GetCurrentUser();

            menuList = foothillMenuControl.GetUserFunctions(usr.UserID);

            //GGFW.Entity.SystemManage.FoothillMenu menu = GetMenu(MenuID); 

            //ExtTreeNode n = new ExtTreeNode();
            list = new List<ExtTreeNode>();

            List<GGFW.Entity.SystemManage.FoothillMenu> tmpList = GetSubMenuList(MenuID.ToString());
            for (int i = 0; i < tmpList.Count; i++)
            {
                ExtTreeNode nn = new ExtTreeNode();
                nn.id = Convert.ToInt32(tmpList[i].MenuID);
                nn.text = tmpList[i].Text.ToString();
                nn.icon = tmpList[i].Icon;
                nn.iconCls = tmpList[i].IconCls;
                nn.href = tmpList[i].Href;
                nn.hrefTarget = tmpList[i].HrefTarget;
                nn.expanded = true;

                list.Add(nn);

                GetChildMenu(nn);
            }


            //if (menu != null)
            //{ 
            //    n.id = Convert.ToInt32(menu.MenuID);
            //    n.text = menu.Text;
            //    n.icon = menu.Icon;
            //    n.iconCls = menu.IconCls;
            //    n.href = menu.Href;
            //    n.hrefTarget = menu.HrefTarget;
            //    n.expanded = true;

            //    GetChildMenu(n);


            //    list.Add(n);
            //}
            if (list != null)
            {
                // 返回json格式
                json = JsonConverter.SerializeObject(list);
            }
            Response.Write(json);
            Response.End();

        }



        private GGFW.Entity.SystemManage.FoothillMenu GetMenu(string MenuID)
        {
            if (menuList.Count <= 0)
                return null;
            return menuList.Where(p => p.MenuID == MenuID).ToList<GGFW.Entity.SystemManage.FoothillMenu>()[0];

        }

        private List<GGFW.Entity.SystemManage.FoothillMenu> GetSubMenuList(string MenuID)
        {
            return menuList.Where(p => p.ParentMenuID == MenuID).ToList<GGFW.Entity.SystemManage.FoothillMenu>();
        }

        private void GetChildMenu(ExtTreeNode n)
        {
            List<GGFW.Entity.SystemManage.FoothillMenu> tmpList = GetSubMenuList(n.id.ToString());

            for (int i = 0; i < tmpList.Count; i++)
            {
                ExtTreeNode nn = new ExtTreeNode();
                nn.id = Convert.ToInt32(tmpList[i].MenuID);
                nn.text = tmpList[i].Text.ToString();
                nn.icon = tmpList[i].Icon;
                nn.iconCls = tmpList[i].IconCls;
                nn.href = "/" + tmpList[i].Href;
                nn.hrefTarget = tmpList[i].HrefTarget;
                nn.expanded = true;

                if (n.children == null)
                    n.children = new List<ExtTreeNode>();

                n.children.Add(nn);

                GetChildMenu(nn);
            }
        }
    }


    public class ExtTreeNode
    {
        private bool _leaf;

        public int id { get; set; }
        public string text { get; set; }
        public List<ExtTreeNode> children = new List<ExtTreeNode>();
        public bool leaf
        {
            get
            {
                if (children == null)
                    return true;
                else if (children.Count > 0)
                    return false;
                else
                    return true;
            }
            set { _leaf = value; }

        }
        public string icon { get; set; }
        public string href { get; set; }
        public string hrefTarget { get; set; }
        public string iconCls { get; set; }
        public bool expanded { get; set; }

    }
}
